<?php

namespace app\api\controller\news;

use app\service\news\VnNewsService;
use hunma\basic\ApiResponse;
use hunma\basic\BaseController;
use support\Log;

class VnNewsController extends BaseController
{
    protected VnNewsService $service;

    public function __construct()
    {
        $this->service = new VnNewsService();
    }

    // 获取以及同步每日新闻
    public function sync()
    {
        try {
            $list = $this->service->fetchDaily(5);
            Log::channel('ApiResponse')->info("越南新闻每日抓取完成", [
                'count' => count($list),
                'date' => date('Y-m-d')
            ]);

            // 同步到越南站通
            $this->service->syncToYzt();
            Log::channel('ApiResponse')->info("越南新闻同步到越南站通完成", [
                'date' => date('Y-m-d')
            ]);
            echo '越南新闻同步到越南站通完成';
        } catch (\Throwable $e) {
            Log::channel('ApiResponse')->error("越南新闻每日抓取失败", [$e->getMessage()]);
        }
    }
}
